package com.danskin.wechat.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.danskin.wechat.dao.CustCouponRecordDao;
import com.danskin.wechat.dao.CustOrderDetailDao;
import com.danskin.wechat.dao.CustOrderRecordDao;
import com.danskin.wechat.dao.CustPointRecordDao;
import com.danskin.wechat.entity.AccountLog;
import com.danskin.wechat.entity.CustCouponRecord;
import com.danskin.wechat.entity.CustOrderDetail;
import com.danskin.wechat.entity.CustOrderRecord;
import com.danskin.wechat.entity.CustPointRecord;
import com.danskin.wechat.entity.example.CustCouponRecordExample;
import com.danskin.wechat.entity.example.CustOrderDetailExample;
import com.danskin.wechat.entity.example.CustOrderRecordExample;
import com.danskin.wechat.entity.example.CustOrderRecordExample.Criteria;
import com.danskin.wechat.entity.example.CustPointRecordExample;
import com.danskin.wechat.log.dao.AccountLogDao;
import com.danskin.wechat.service.AccountLogService;
import com.danskin.wechat.util.Page;

import net.sf.jsqlparser.util.AddAliasesVisitor;
@Service
public class AccountLogServiceImpl extends BaseServiceImpl implements AccountLogService {
	
	@Autowired
	private AccountLogDao accountLogDao;
	
	@Autowired
	private CustOrderRecordDao orderRecordDao;
	
	@Autowired
	private CustOrderDetailDao orderDetailDao;
	
	@Autowired
	private CustPointRecordDao pointRecordDao;
	
	@Autowired
	private CustCouponRecordDao couponRecordDao;
	


	@Override
	public List<AccountLog> listPageGetAccountLog(Page page) {
		Map<String, Object> map = page.getMap();
		if(!map.containsKey("tableName")){
			map.put("tableName", "log_"+new SimpleDateFormat("yyyyMM").format(new Date()));
		}
		List<AccountLog> list = accountLogDao.listPageGetAccountLog(page);
		return list;
	}

	/**
	 * 数据迁移备份
	 * 积分数据：cust_point_record
		交易订单数据：cust_order_record、cust_order_detail
		优惠券数据：cust_coupon_record
	 */
	
	@Override
	public int MoveScrmToLogForBackUp(Map<String, Object> map) {
		Date begin = (Date)map.get("beginTime");
		Integer mpId=Integer.parseInt(String.valueOf(map.get("mpId")));
		int i=0;
		int k=0;
		int z=0;
		int r=0;
		if(null!=begin){
			CustOrderRecordExample orderRecordExample=new CustOrderRecordExample();
			Criteria orderRecordCriteria = orderRecordExample.createCriteria();
			orderRecordCriteria.andCreatedTimeLessThanOrEqualTo(begin);
			List<CustOrderRecord> orderRecordList = orderRecordDao.selectByExample(orderRecordExample);
			if(orderRecordList.size()>0){
			i=accountLogDao.insertCustOrderRecordBatch(orderRecordList);
			 if(i>0){
				 orderRecordDao.deleteByExample(orderRecordExample);
			}
			}
			
			CustOrderDetailExample orderDetailExample=new CustOrderDetailExample();
			com.danskin.wechat.entity.example.CustOrderDetailExample.Criteria orderDetailcriteria = orderDetailExample.createCriteria();
			orderDetailcriteria.andCreatedTimeLessThanOrEqualTo(begin);
			List<CustOrderDetail> orderDetailList = orderDetailDao.selectByExample(orderDetailExample);
			if(orderDetailList.size()>0){
			k=accountLogDao.insertCustOrderDetailBatch(orderDetailList);
			if(k>0){
				orderDetailDao.deleteByExample(orderDetailExample);
			}
			}
			
			CustCouponRecordExample couponRecordExample=new CustCouponRecordExample();
			com.danskin.wechat.entity.example.CustCouponRecordExample.Criteria couponRecordCriteria = couponRecordExample.createCriteria();
			couponRecordCriteria.andCreatedTimeLessThanOrEqualTo(begin);
			List<CustCouponRecord> CouponRecordList = couponRecordDao.selectByExample(couponRecordExample);
			if(CouponRecordList.size()>0){
			z=accountLogDao.insertCustCouponRecordBatch(CouponRecordList);
			if(z>0){
				couponRecordDao.deleteByExample(couponRecordExample);
			}
			}
			
			CustPointRecordExample pointRecordExample=new CustPointRecordExample();
			com.danskin.wechat.entity.example.CustPointRecordExample.Criteria pointRecordCriteria = pointRecordExample.createCriteria();
			pointRecordCriteria.andCreatedTimeLessThanOrEqualTo(begin);
			List<CustPointRecord> pointRecordList = pointRecordDao.selectByExample(pointRecordExample);
			if(pointRecordList.size()>0){
			r=	accountLogDao.insertCustPointRecordBatch(pointRecordList);
			if(r>0){
				pointRecordDao.deleteByExample(pointRecordExample);
			}
			}
			add("数据备份","数据模块");
			
		}
		if(i>0||k>0||z>0||r>0){
			return 1;
		}else{
			return 0;
		}
		
		
		
	}

}
